﻿using Microsoft.EntityFrameworkCore;
using ProjectCore.Common;
using ProjectCore.Domain.DomainService;
using ProjectCore.Domain.Model.Entity;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ProjectCore.Infrastructure.DomainService
{
  public  class RolesDomainService: IRolesDomainService
    {
        private readonly IBaseRepository<Roles> _baseRepository;
        private readonly MyContext myContext;
        private readonly IBaseRepository<RoleMenus> baseRepository1;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userRepository"></param>
        public RolesDomainService(IBaseRepository<Roles> baseRepository,  MyContext _myContext, IBaseRepository<RoleMenus> _baseRepository1)
        {
            _baseRepository = baseRepository;
            baseRepository1 = _baseRepository1;
            myContext = _myContext;
        }
        public async Task<int> Add(Roles roles)
        {
            return await _baseRepository.AddEntityAsync(roles);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<Roles> Edit(int Id)
        {
            return await myContext.Roles.FindAsync(Id);
        }

        public async Task<List<RoleMenus>> GetRoleMenus(int Id)
        {
            try
            {
                Expression<Func<RoleMenus, bool>> where = e => e.RoleID.Equals(Id);
                return await baseRepository1.WhereLoadEntityList(where);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<Roles>> GetRoles()
        {
            return await _baseRepository.LoadEntityAllAsync();
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> IsDel(int Id)
        {
            Eval eval = myContext.Evals.Where(x => x.EvalID.Equals(Id)).FirstOrDefault();
            eval.IsDeleted = true;
            return await myContext.SaveChangesAsync();
            //string sql = $"update Roles set IsDeleted = 1 where RoleID = {Id}";
            //return myContext.Database.ExecuteSqlCommand(sql);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public async Task<int> Upt(Roles roles)
        {
            return await _baseRepository.UpdateEntityAsync(roles);
        }

        public async Task<int> UptRoleMenus(List<int> vs, int Id)
        {
            Expression<Func<RoleMenus, bool>> where = e => e.RoleID.Equals(Id);
            await baseRepository1.WhereLoadEntityAsync(where);

            List<RoleMenus> roleMenus = new List<RoleMenus>();
            for (int i = 0; i < vs.Count - 1; i++)
            {
                RoleMenus roleMenus1 = new RoleMenus();
                roleMenus1.MenuID = vs[i];
                roleMenus1.RoleID = Id;
                roleMenus1.Add(roleMenus);
            }
            return await baseRepository1.AddRangeListAsync(roleMenus);
        }
    }
}
